package com.liqi.common.config;

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.AES;
import com.liqi.common.utils.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: lgh
 * @Date: 2023-05-25
 * @Description:
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes({Object.class})
public class Sm4TypeHandler extends BaseTypeHandler<Object> {
    // 需要用16位key加密
    public static String KEY = "fTlJFFvlUaBUrFkZ";

    // AES加密
    private static AES aes = new AES(KEY.getBytes());

//    @Override
//    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
//        if(StringUtils.isBlank((String) o)){
//            return;
//        }
//        preparedStatement.setString(i, SmUtil.sm4(KEY.getBytes()).encryptHex((String) o));
//    }
//
//    @Override
//    public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
//        String columnValue = resultSet.getString(s);
//        if (StringUtils.isBlank(columnValue)) {
//            return null;
//        }
//        return SmUtil.sm4(KEY.getBytes()).decryptStr(columnValue);
//    }
//
//    @Override
//    public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
//        String columnValue = resultSet.getString(i);
//        if (StringUtils.isBlank(columnValue)) {
//            return null;
//        }
//        return SmUtil.sm4(KEY.getBytes()).decryptStr(columnValue);
//    }
//
//    @Override
//    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
//        String columnValue = callableStatement.getString(i);
//        if (StringUtils.isBlank(columnValue)) {
//            return null;
//        }
//        return SmUtil.sm4(KEY.getBytes()).decryptStr(columnValue);
//    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
//        if(StringUtils.isBlank((String) o)){
//            return;
//        }
        preparedStatement.setString(i,aes.encryptHex((String) o));
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String columnValue = resultSet.getString(s);
        if (StringUtils.isBlank(columnValue)) {
            return columnValue;
        }
        return aes.decryptStr(columnValue);
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String columnValue = resultSet.getString(i);
        if (StringUtils.isBlank(columnValue)) {
            return columnValue;
        }
        return aes.decryptStr(columnValue);
    }

    @Override
    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String columnValue = callableStatement.getString(i);
        if (StringUtils.isBlank(columnValue)) {
            return columnValue;
        }
        return aes.decryptStr(columnValue);
    }
    public static void main(String[] args) {
        String key = "1234567887654321";
        AES aes = new AES(key.getBytes());
        String s = aes.encryptHex("hello");
        System.out.println(s);
        System.out.println(aes.decryptStr(s));
    }
}
